<template>
  <div class="user-profile">
    <!-- 头部导航栏 -->
    <van-nav-bar title="个人信息" left-arrow @click-left="$router.back()">
    </van-nav-bar>
    <input type="file" hidden ref="file" @change="onFileChange">
    <!-- 个人信息 -->
    <van-cell @click="$refs.file.click()" title="头像" is-link>
      <template #default>
        <van-image fit="cover" class="avatar" round :src="user.photo" />
      </template>
    </van-cell>
    <van-cell @click="isUpdateNameShow=true" title="昵称" :value="user.name" is-link />
    <van-cell @click="isUpdateGenderShow=true" title="性别" :value="user.gender===0 ? '男' : '女'" is-link />
    <van-cell @click="isUpdateBirthdayShow=true" title="生日" :value="user.birthday" is-link />
    <!-- 编辑昵称 -->
    <van-popup v-model="isUpdateNameShow" position="bottom" style="height: 100%">
      <UpdateName v-if="isUpdateNameShow" @close="isUpdateNameShow=false" v-model="user.name"></UpdateName>
    </van-popup>
    <!-- 编辑性别 -->
    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <UpdateGender v-if="isUpdateGenderShow" v-model="user.gender" @close="isUpdateGenderShow=false"></UpdateGender>
    </van-popup>
    <!-- 编辑生日 -->
    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
      <UpdateBirthday v-if="isUpdateBirthdayShow" v-model="user.birthday" @close="isUpdateBirthdayShow=false"></UpdateBirthday>
    </van-popup>
    <!-- 编辑头像 -->
    <van-popup v-model="isUpdatePhotoShow" position="bottom" style="height: 100%">
      <UpdatePhoto v-if="isUpdatePhotoShow" @update-photo="user.photo=$event" :img="img" @close="isUpdatePhotoShow=false"></UpdatePhoto>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/users.js'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserProfile',
  data() {
    return {
      user: {}, // 用户的个人信息
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow: false,
      img: null
    }
  },
  created() {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        // console.log(data)
        this.user = data.data
      } catch (err) {
        this.$toast.fail('获取信息失败')
      }
    },
    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文件对象获取blob数据
      const data = window.URL.createObjectURL(file)
      console.log(data)
      this.isUpdatePhotoShow = true
      this.img = data
      // file-input 如果选了同一个文件不会触发change事件
      // 解决办法就是 每次使用完毕  就把它的value清空
      this.$refs.file.value = ''
    }
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #3296fa;
  /deep/.van-icon {
    color: #fff;
  }
}
/deep/.van-nav-bar__title {
  color: #fff;
}
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
